<?php

/**
 * Created by PhpStorm.
 * User: raymon
 * Date: 17-7-18
 * Time: 下午9:19
 */
class Users_model extends MY_Model
{
    public function __construct()
    {
        $this->load->database();

    }

    public function get_user($uid)
    {
        $this->db->where('id', $uid);
        $query = $this->db->get($this->db->dbprefix('users'));
        return $query->row_array();
    }

    public function update_user($uid, $data)
    {
        $this->db->where('id', $uid);
        if ($this->db->update($this->db->dbprefix('users'), $data)) {
            return true;
        } else {
            return false;
        }
    }

    //根据name获取uid
    public function get_uid_by_name($name)
    {
        $this->db->where('name', $name);
        $query = $this->db->get($this->db->dbprefix('users'));
        $result = $query->row_array();
        if (!empty($result)) {
            return $result['id'];
        } else {
            return flase;
        }
    }

    //验证uid是否存在
    public function check_isset($uid)
    {
        $this->db->where('id', $uid);
        $query = $this->db->get($this->db->dbprefix('users'));
        if (empty($query->row_array())) {
            return false;
        } else {
            return true;
        }
    }

    //根据uid校验密码是否正确
    public function check_password($uid, $pw)
    {
        $this->db->where('id', $uid);
        $query = $this->db->get($this->db->dbprefix('users'));
        $result = $query->row_array();
        if ($result['password'] == $pw) {
            return true;
        } else {
            return false;
        }
    }

    public function auth($uid, $pw)
    {
        $this->db->where('id', $uid);
        $query = $this->db->get($this->db->dbprefix('users'));
        $result = $query->row_array();
        if (!empty($result) && $result['password'] == $pw) {
            return true;
        }
    }

    public function login($name, $pw)
    {
        $this->db->where('name', $name);
        $query = $this->db->get($this->db->dbprefix('users'));
        $result = $query->row_array();
        if (!empty($result['id'])) {
            if ($result['password'] == $pw) {
                $result['auth'] = ture;
            }
        }
        return $result;
    }
}